import Vue from 'vue'
import Router from 'vue-router'
import { getToken } from '@/utils/auth'

Vue.use(Router)

import Layout from '@/layout'

export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [{
      path: 'dashboard',
      name: 'Dashboard',
      component: () => import('@/views/dashboard/index'),
      meta: { title: '网络安全形势报表', icon: 'el-icon-s-home' }
    }]
  },
  {
    path: '/urls',
    component: Layout,
    redirect: '/urls',
    name: 'Urls',
    meta: { title: '恶意域名分析', icon: 'el-icon-s-flag' },
    children: [
      {
        path: 'choose',
        name: 'Choose',
        component: () => import('@/views/chooseActs'),
        meta: { title: '域名检测', icon: 'el-icon-s-comment' }
      },
      {
        path: 'asia',
        name: 'Asia',
        component: () => import('@/views/asia'),
        meta: { title: '空间统计', icon: 'el-icon-s-comment' }
      },
      {
        path: 'tim',
        name: 'Tim',
        component: () => import('@/views/tim'),
        meta: { title: '时间统计', icon: 'el-icon-s-comment' }
      },
      {
        path: 'clt',
        name: 'Clt',
        component: () => import('@/views/clt'),
        meta: { title: '内网主机', icon: 'el-icon-s-comment' }
      },
    ]
  },
  {
    path: '/activitys',
    component: Layout,
    redirect: '/activitys',
    name: 'Activitys',
    meta: { title: '模型管理', icon: 'el-icon-s-flag' },
    children: [
      {
        path: 'my',
        name: 'My',
        component: () => import('@/views/myActs'),
        meta: { title: '模型配置', icon: 'el-icon-s-comment' }
      },
      {
        path: 'dataset',
        name: 'Dataset',
        component: () => import('@/views/dataset'),
        meta: { title: '数据集生成', icon: 'el-icon-s-comment' }
      }
    ]
  },
  {
    path: '/out',
    component: Layout,
    redirect: '/out',
    children: [{
      path: 'out',
      name: 'Out',
      component: () => import('@/views/out'),
      meta: { title: '导出日志', icon: 'el-icon-s-home' }
    }]
  },
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

const authUrls = ['/activitys/my']

router.beforeEach((to, from, next) => {
  // 看 to.path 是否是权限访问页面
  if (!authUrls.includes(to.path)) {
    // 非权限页面，直接放行
    next()
    return
  }

  // 是权限页面，需要判断token，看vuex里面有没有token（他这里直接看的localstorage里面的）
  const token = getToken()
  if (token) {
    next()
  } else {
    next('/login')
  }
})

export default router
